package com.example.demo.Mapper;

import com.example.demo.User;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Locale;

// 在程序运行时 框架会自动生成接口的实现类对象，
// 并交给spring的IOC容器管理
@Mapper // 表示这个是MyBatis中的Mapper接口
public interface UserMapper {
    // 写sql语句 - 查询所有用户数据
    // @Select代表写的是select语句
    @Select("select * from t_user")
    public List<User> allUserList();

    //根据ID删除用户
    @Delete("delete from t_user where id= #{id}")
    public void deleteById(Integer id);

    //登录
    // 登录
  // 无法防止sql注入的
  // @Select("select * from t_user where username=${username} and password=${password} ")
  // @Select("select * from t_user where username=${username} and
  // password=${password} ")
  // 可以预防sql注入
  @Select("select * from t_user where id=${id} and phone=${phone} ")
  public List<User> login(@Param("id") String id, @Param("phone") String phone);

    //新增
    @Options(useGeneratedKeys = true, keyProperty = "id")
    //要求返回主键，赋值给user对象的属性id
    @Insert("insert into t_user (name,age,gender) values (#{name},#{age},#{gender})")
    public  void addUser(User user);

    // 编辑
    @Update("update t_user set name=#{name}, age=#{age}, gender=#{gender}, phone=#{phone} where id=#{id}")
    public void updateUser(User user);

    //组合查询
    @Select("select * from t_user"+
            "where name like '%',#{name},'%'"+
            "and gender = #{gender}"+
            "and register_time between #{beginTime} and #{endTime}"+
            "order by register_time desc")
    public List<User> getUserList(
             @Param("name")  String name,
             @Param("gender") short gender,
             @Param("beginTime")LocalDateTime beginTime,
             @Param("endTime") LocalDateTime endTime);

}
